package protocol;

import java.util.TreeMap;

import reactor.ConnectionHandler;

public class UsersConections {

	private static TreeMap<Integer, ConnectionHandler> conections = new TreeMap<Integer, ConnectionHandler>();
	
	
	/*
	 * returns the connection handler attached to the given userId
	 * on fail: returns null
	 */
	public static ConnectionHandler getHandler(int userId)
	{
		return conections.get(userId);
	}
	
	/*
	 * returns -1 on fail.
	 * 1 if there was mapping before adding the mapping.
	 * and 0 otherwize. 
	 */
	public static int addConnection(int id, ConnectionHandler ch)
	{
		//System.out.println("before adding: " + conections.size());
		if(ch != null && id >= 0)
		{
			if(conections.put(id, ch) != null)
			{
			//	System.out.println("after add: " + conections.size());
				return 1;
			}
			else
			{
//				System.out.println("after add: " + conections.size());
				return 0;
			}
		}
//		System.out.println("after add: " + conections.size());
		return -1;
	}
	
	/*
	 * removes a mapping from the table. 
	 */
	public static void removeConnection(int id)
	{
//		System.out.println("before remove: " + conections.size());
		conections.remove(id);
//		System.out.println("after remove: " + conections.size());
	}
	
	
}
